<script setup>

</script>

<template>
  <div class="add-city-container">
    <h2>添加城市</h2>
    <el-button type="success" style="float: right" @click="goBack">返回</el-button>
    <el-form :model="cityForm" label-width="120px" :rules="rules" ref="cityFormRef">
      <el-form-item label="城市名称 (*)" prop="cityName">
        <el-input v-model="cityForm.cityName"></el-input>
      </el-form-item>
      <el-form-item label="高德地图城市编码 (*)" prop="gaodeCode">
        <el-input v-model="cityForm.gaodeCode"></el-input>
        <el-link type="primary" @click="getGaodeCode">高德地图城市编码</el-link>
      </el-form-item>
      <el-form-item label="城市中心经度 (*)" prop="longitude">
        <el-input v-model="cityForm.longitude"></el-input>
        <el-link type="primary" @click="getCoordinates">高德地图拾取器</el-link>
      </el-form-item>
      <el-form-item label="城市中心纬度 (*)" prop="latitude">
        <el-input v-model="cityForm.latitude"></el-input>
      </el-form-item>
      <el-form-item label="下单风险上限值 (*)" prop="riskLimit">
        <el-input v-model="cityForm.riskLimit"></el-input>
      </el-form-item>
      <el-form-item>
        <el-checkbox v-model="cityForm.createAndOpen">创建并开通</el-checkbox>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">确认</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<style scoped>

</style>
<script>export default {
  data() {
    return {
      cityForm: {
        cityName: '',
        gaodeCode: '',
        longitude: '',
        latitude: '',
        riskLimit: '',
        createAndOpen: false
      },
      rules: {
        cityName: [
          { required: true, message: '请输入城市名称', trigger: 'blur' }
        ],
        gaodeCode: [
          { required: true, message: '请输入高德地图城市编码', trigger: 'blur' }
        ],
        longitude: [
          { required: true, message: '请输入城市中心经度', trigger: 'blur' }
        ],
        latitude: [
          { required: true, message: '请输入城市中心纬度', trigger: 'blur' }
        ],
        riskLimit: [
          { required: true, message: '请输入下单风险上限值', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    goBack() {
      this.$router.go(-1); // 返回到上一个页面
    },
    getGaodeCode() {
      console.log('点击获取高德地图城市编码');
      // 这里添加调用高德地图 API 获取编码的逻辑
    },
    getCoordinates() {
      console.log('点击高德地图拾取器');
      // 这里添加调用高德地图 API 获取经纬度的逻辑
    },
    submitForm() {
      this.$refs.cityFormRef.validate((valid) => {

        if (valid) {
          console.log('提交表单数据', this.cityForm);

          // 这里添加调用后端接口提交数据的逻辑
          // 提交成功后可以跳转到城市管理页面
          this.$router.push('/city-management');
        } else {
          console.log('表单验证失败');
          return false;
        }
      });
    },
    resetForm() {
      this.$refs.cityFormRef.resetFields();
    }
  }
};
</script>
<style scoped>
.add-city-container {
  padding: 20px;
}
</style>